package com.ultrapower.bean;

import com.ultrapower.util.BytesUtils;
import com.ultrapower.util.CommonUtils;
import com.ultrapower.util.HBaseTables;
import com.ultrapower.util.RowKeyUtils;

public class StringMetaDataBean {
	private String agentId;
	private long startTime;

	private int stringId;

	private String stringValue;

	public StringMetaDataBean() {
	}

	public StringMetaDataBean(String agentId, long startTime, int stringId) {
		if (agentId == null) {
			throw new NullPointerException("agentId must not be null");
		}
		this.agentId = agentId;
		this.stringId = stringId;
		this.startTime = startTime;
	}

	public void readRowKey(byte[] rowKey) {
		this.agentId = BytesUtils.safeTrim(BytesUtils.toString(rowKey, 0, HBaseTables.AGENT_NAME_MAX_LEN));
		this.startTime = CommonUtils.recoveryTimeMillis(readTime(rowKey));
		this.stringId = readKeyCode(rowKey);
	}

	private static long readTime(byte[] rowKey) {
		return BytesUtils.bytesToLong(rowKey, HBaseTables.AGENT_NAME_MAX_LEN);
	}

	private static int readKeyCode(byte[] rowKey) {
		return BytesUtils.bytesToInt(rowKey, HBaseTables.AGENT_NAME_MAX_LEN + BytesUtils.LONG_BYTE_LENGTH);
	}

	public byte[] toRowKey() {
		return RowKeyUtils.getMetaInfoRowKey(this.agentId, this.startTime, this.stringId);
	}

	@Override
	public String toString() {
		return "StringMetaDataBean [agentId=" + agentId + ", startTime=" + startTime + ", stringId=" + stringId
				+ ", stringValue=" + stringValue + "]";
	}

	public void setStringValue(String stringValue) {
		this.stringValue=stringValue;
	}
	
	

}
